<template>
  <el-dialog
    title="恢复删除点"
    v-dialogDrag
    :visible.sync="dialogVisible"
    width="500px"
    :before-close="handleCloseDialog"
    custom-class="refresh-del-point-dialog"
  >
    <div class="dialog-content">
      <!-- 选择线路部分 -->
      <div class="section">
        <div class="section-header">
          <i class="el-icon-connection"></i>
          <span class="section-title">选择线路</span>
        </div>
        <el-form ref="menuForm" label-width="100px" size="medium" class="form-container">
          <el-form-item label="线路选择">
            <el-select 
              v-model="selectRestore" 
              placeholder="请选择要恢复的线路" 
              @change="handleChangeRestore"
              style="width: 100%"
              clearable
              filterable
            >
              <el-option
                v-for="item in restoreList"
                :key="item.id"
                :label="item.pointName"
                :value="item.codeID"
              >
                <div class="option-item">
                  <span class="option-name">{{ item.pointName }}</span>
                </div>
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      
      <!-- 删除点列表部分 -->
      <div class="section" v-if="selectRestore">
        <div class="section-header">
          <i class="el-icon-delete"></i>
          <span class="section-title">删除点列表</span>
          <el-tag v-if="deletePointList.length > 0" size="mini" type="info">
            共 {{ deletePointList.length }} 个删除点
          </el-tag>
        </div>
        
        <!-- 加载状态 -->
        <div v-if="loading" class="loading-container">
          <i class="el-icon-loading"></i>
          <span>正在加载删除点数据...</span>
        </div>
        
        <!-- 空状态 -->
        <div v-else-if="deletePointList.length === 0" class="empty-container">
          <i class="el-icon-document-delete"></i>
          <p>该线路暂无删除点</p>
        </div>
        
        <!-- 删除点列表 -->
        <div v-else class="delete-points-container">
          <div 
            v-for="(item, index) in deletePointList" 
            :key="index" 
            class="delpoint-item"
            :class="{ 'restoring': item.restoring }"
          >
            <div class="point-info">
              <div class="point-name">
                <el-tooltip effect="dark" :content="item.val" placement="top">
                  <span class="name-text">{{ truncateText(item.val, 20) }}</span>
                </el-tooltip>
              </div>
              <div class="point-time">
                <i class="el-icon-time"></i>
                <span>{{ formatTime(item.WATCHTIME) }}</span>
              </div>
            </div>
            <div class="point-actions">
              <el-button 
                type="success" 
                size="mini" 
                :loading="item.restoring"
                @click="handleRestorePoint(item, index)"
                class="restore-btn"
              >
                {{ item.restoring ? '恢复中' : '恢复' }}
              </el-button>
            </div>
          </div>
        </div>
      </div>
      
      <!-- 操作提示 -->
      <div class="tips-section">
        <el-alert
          title="操作提示"
          type="info"
          :closable="false"
          description="选择线路后，可以查看该线路下已删除的点，点击恢复按钮可将删除点恢复。"
          show-icon
        >
        </el-alert>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { getLineHideData, LineHideDataDel } from "@/api/https";

export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    restoreList: {
      type: Array,
      default: () => {
        return [];
      }
    }
  },
  mounted() {},
  watch: {
    visible(newVal) {
      if (newVal) {
        this.dialogVisible = newVal;
        // 对话框打开时重置状态
        if (newVal) {
          this.selectRestore = '';
          this.deletePointList = [];
          this.loading = false;
        }
      }
    },
    dialogVisible(newVal) {
      this.$emit('update:visible', newVal);
    }
  },
  data() {
    return {
      dialogVisible: this.visible,
      deletePointList: [],
      selectRestore: '',
      loading: false
    };
  },
  methods: {
    handleChangeRestore(val) {
      if (!val) {
        this.deletePointList = [];
        return;
      }
      
      this.selectRestore = val;
      this.loading = true;
      
      getLineHideData({
        LineID: this.restoreList[0].lineID,
        codeID: val
      }).then(res => {
        this.loading = false;
        if (res.isSuccess && res.data.length > 0) {
          // 为每个删除点添加状态标记
          this.deletePointList = res.data.map(item => ({
            ...item,
            restoring: false
          }));
          this.$message.success(`成功加载 ${res.data.length} 个删除点`);
        } else {
          this.deletePointList = [];
          this.$message.info('该线路暂无删除点');
        }
      }).catch(() => {
        this.loading = false;
        this.$message.error('加载删除点数据失败');
      });
    },
    
    handleRestorePoint(data, index) {
      this.$confirm(`确定要恢复删除点 "${this.truncateText(data.val, 15)}" 吗？`, '恢复确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 标记为恢复中状态
        this.$set(this.deletePointList[index], 'restoring', true);
        
        LineHideDataDel({
          ID: data.id
        }).then(res => {
          if (res.isSuccess) {
            this.$message.success('恢复成功');
            this.deletePointList.splice(index, 1);
          } else {
            this.$set(this.deletePointList[index], 'restoring', false);
            this.$message.error(res.errorMessage || '恢复失败');
          }
        }).catch(() => {
          this.$set(this.deletePointList[index], 'restoring', false);
          this.$message.error('恢复失败，请重试');
        });
      }).catch(() => {
        // 用户取消操作
      });
    },
    
    handleCloseDialog() {
      this.dialogVisible = false;
      this.$emit('closeRefreshDel');
    },
    
    // 格式化时间显示
    formatTime(timeStr) {
      if (!timeStr) return '未知时间';
      try {
        return new Date(timeStr).toLocaleString();
      } catch (e) {
        return timeStr;
      }
    },
    
    // 截断长文本
    truncateText(text, maxLength) {
      if (!text) return '';
      return text.length > maxLength ? text.substring(0, maxLength) + '...' : text;
    }
  }
};
</script>

<style scoped>
.dialog-content {
  padding: 0 10px;
}

.section {
  margin-bottom: 20px;
}

.section-header {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
  padding-bottom: 8px;
  border-bottom: 1px solid #ebeef5;
}

.section-header i {
  margin-right: 8px;
  color: #409EFF;
}

.section-title {
  font-weight: 600;
  font-size: 14px;
  margin-right: 10px;
}

.form-container {
  padding: 0 10px;
}

.option-item {
  display: flex;
  justify-content: space-between;
}

.option-name {
  font-weight: 500;
}

.option-id {
  color: #909399;
  font-size: 12px;
}

.loading-container, .empty-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 30px 0;
  color: #909399;
}

.loading-container i, .empty-container i {
  font-size: 40px;
  margin-bottom: 10px;
}

.delete-points-container {
  max-height: 300px;
  overflow-y: auto;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  padding: 10px;
}

.delpoint-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 15px;
  border-bottom: 1px solid #f0f0f0;
  transition: all 0.3s;
}

.delpoint-item:last-child {
  border-bottom: none;
}

.delpoint-item:hover {
  background-color: #f5f7fa;
}

.delpoint-item.restoring {
  background-color: #f0f9ff;
  opacity: 0.7;
}

.point-info {
  flex: 1;
}

.point-name {
  font-weight: 500;
  margin-bottom: 5px;
}

.name-text {
  color: #303133;
}

.point-time {
  display: flex;
  align-items: center;
  font-size: 12px;
  color: #909399;
}

.point-time i {
  margin-right: 5px;
}

.restore-btn {
  min-width: 70px;
}

.tips-section {
  margin-top: 20px;
}

</style>

